/**
 * Created by PanJiaChen on 16/11/18.
 */
import {Options} from './constant.js'
/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
  const valid_map = ['admin', 'editor']
  return valid_map.indexOf(str.trim()) >= 0
}
/**
 * 判断数据是否为空
 * @param {string} obj
 * @returns {Boolean}
 */
export function isEmpty(obj) {
  if (typeof obj == 'undefined' || obj == null || obj == '' || obj == 'null') {
    return true
  } else {
    return false
  }
}
/**
 * 默认返回yyyy-MM-dd HH-mm-ss
 * 可传第二个参数false，返回yyyy-MM-dd
 * 可传第三个参数，true 返回yyyy年MM月dd日
 * @param {*} str
 */
export function dateFormatter(str) {
  // console.log('arguments[1]', arguments[1])
  if (str != undefined) {
    var hasTime = arguments[1] != false
    var hasType = arguments[2]
    var d = new Date(str)
    var year = d.getFullYear()
    var month = (d.getMonth() + 1) < 10 ? '0' + (d.getMonth() + 1) : (d.getMonth() + 1)
    var day = d.getDate() < 10 ? '0' + d.getDate() : d.getDate()
    var hour = d.getHours() < 10 ? '0' + d.getHours() : d.getHours()
    var minute = d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes()
    var second = d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds()
    // console.log('hasTime', hasTime)
    if (hasTime) {
      if (hasType) {
        return `${month}月${day}日 ${hour}时${minute}分${second}秒`
      }
      return [year, month, day].join('-') + ' ' + [hour, minute, second].join(':')
    } else {
      if (hasType) {
        return `${month}月${day}日`
      }
      return [year, month, day].join('-')
    }
  }
}
/**
 * 默认返回 [yyyy-MM-01,yyyy-MM-dd]   整月时间
 * @param {*} date
 * @param {Boolean} hasTime
 */
export function getTotalMonth(date, hasTime = false) {
  let newDate = new Date(date)
  let year = newDate.getFullYear()
  let month = newDate.getMonth() + 1 < 10 ? '0' + (newDate.getMonth() + 1) : newDate.getMonth() + 1
  var hour = newDate.getHours() < 10 ? '0' + newDate.getHours() : newDate.getHours()
  var minute = newDate.getMinutes() < 10 ? '0' + newDate.getMinutes() : newDate.getMinutes()
  var second = newDate.getSeconds() < 10 ? '0' + newDate.getSeconds() : newDate.getSeconds()
  let timestamp = new Date(`${year}/${newDate.getMonth() + 2}/01`).getTime() - 24 * 60 * 60 * 1000

  if (hasTime) {
    return [`${year}-${month}-01 ${hour}:${minute}:${second}`, dateFormatter(timestamp, true)]
  } else {
    return [`${year}-${month}-01`, dateFormatter(timestamp, false)]
  }
}
/**
 * 规则状态
 */
export function ruleStatusFlr(value) {
  var stauts = value
  switch (value) {
    case 0:
      stauts = '禁用'
      break
    case 1:
      stauts = '启用'
      break
    default:
      break
  }
  return stauts
}
/**
 * 操作类型
 */
export function operateTypeFlr(value) {
  var type = value
  switch (value) {
    case 0:
      type = '排班'
      break
    default:
      break
  }
  return type
}
/**
 * 规则类型
 * @param {*} value
 */
export function ruleTypeFlr(value) {
  var ruleType = value
    let obj = Options.ruleTypeOptions.find(item =>item.value === value)
    ruleType = obj && obj.label
  return ruleType
}

/**
 * 会诊主题路径名
 * @param {*} value
 */
export function formatApplyThemePathName(value) {
  var applyTheme = value
  switch (value) {
    case '0':
      applyTheme = '远程影像申请'
      break
    case '1':
      applyTheme = '临床会诊申请'
      break
    case '2':
      applyTheme = '远程门诊申请'
      break
    case '3':
      applyTheme = 'mdt会诊申请'
      break
    case '4':
      applyTheme = '远程超声申请'
      break
    case '5':
      applyTheme = '远程心电申请'
      break
    case '6':
      applyTheme = '远程病理申请'
      break
    case '7':
      applyTheme = '双向转诊'
      break
    case '8':
      applyTheme = '远程培训'
      break
    case '9':
      applyTheme = '国际会诊'
      break
    default:
      break
  }
  return applyTheme
}
// /**
//  * 会诊主题 反获取
//  * @param {*} value
//  */
export function formatApplyThemeReverse(value) {
  var applyTheme = value
  switch (value) {
    case '远程影像申请':
      applyTheme = '0'
      break
    case '临床会诊申请':
      applyTheme = '1'
      break
    case '远程门诊申请':
      applyTheme = '2'
      break
    case 'mdt会诊申请':
      applyTheme = '3'
      break
    case '远程超声申请':
      applyTheme = '4'
      break
    case '远程心电申请':
      applyTheme = '5'
      break
    case '远程病理申请':
      applyTheme = '6'
      break
    case '双向转诊':
      applyTheme = '7'
      break
    case '远程培训':
      applyTheme = '8'
      break
    case '国际会诊':
      applyTheme = '9'
      break
    default:
      break
  }
  return applyTheme
}

/**
 * 会诊方式
 * @param {*} value
 */
export function formatConsultationMethod(value) {
  var consultationMethod = value
  switch (value) {
    case '0':
      consultationMethod = '非指名会诊'
      break
    case '1':
      consultationMethod = '指名会诊'
      break
    default:
      break
  }
  return consultationMethod
}
/**
 * 预约会诊
 * @param {*} value
 */
export function formatAppointmentType(value) {
  var appointmentType = value
  switch (value) {
    case '0':
      appointmentType = '非预约'
      break
    case '1':
      appointmentType = '预约'
      break
    default:
      break
  }
  return appointmentType
}
/**
 * 会诊类型
 * @param {*} value
 */
export function formatConsultationType(value) {
  var consultationType = value
  switch (value) {
    case '0':
      consultationType = '交互式'
      break
    case '1':
      consultationType = '普通'
      break
    case '2':
      consultationType = '急诊'
      break
    default:
      break
  }
  return consultationType
}
/**
 * 转诊类型
 * 0：向上转诊，1：向下转诊，2：住院上转
 * @param {*} value
 */
export function formatApplyType(value) {
  var applyType = value
  switch (value) {
    case 0:
      applyType = '向上转诊'
      break
    case 1:
      applyType = '向下转诊'
      break
    case 2:
      applyType = '住院上转'
      break
    default:
      break
  }
  return applyType
}
/**
 * 转诊状态 0:暂存,1:退回,2:未审核,3:拒绝,4:未安排、5:未接收,6:已完成,7:取消
 * @param {*} value
 */
export function formatApplyStatus(value) {
  var applyStatus = parseInt(value)
  switch (value) {
    case 0:
      applyStatus = '暂存'
      break
    case 1:
      applyStatus = '退回'
      break
    case 2:
      applyStatus = '未审核'
      break
    case 3:
      applyStatus = '拒绝'
      break
    case 4:
      applyStatus = '未安排'
      break
    case 5:
      applyStatus = '未接受'
      break
    case 6:
      applyStatus = '已完成'
      break
    case 7:
      applyStatus = '取消'
      break
    default:
      break
  }
  return applyStatus
}
/**
 * 机房状态
 */
export function roomStatusFlr(value) {
  var stauts = value
  switch (value) {
    case 0:
      stauts = '停用'
      break
    case 1:
      stauts = '启用'
      break
    default:
      break
  }
  return stauts
}
/**
 * 身份证号校验
 * @param {string} value
 * @returns {Boolean}
 */
export function isIdCard(value) {
  return /^[1-9]\d{5}(18|19|20|21|22)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|[Xx])$/.test(value)
  // return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(value)
}
/**
 * 电话好号码（手机号及固定电话）验证
 * @param {string} value
 * @returns {Boolean}
 */
export function isPhone(value) {
  return /^1(3|4|5|6|7|8|9)\d{9}$/.test(value) || /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/.test(value)
}

export function imgReg(file) {
  return /\.(png|jpg|gif|jpeg)$/.test(file)
}

/**
 * 获取 UUID
 * @returns uuid
 */
export function getNewUUID() {
  return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
    (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
  )
}

/**
 * 通过身份证号获取性别
 * @param {*} id 身份证号
 * @returns 性别
 */
 export function sexById(id) {
  // 1 男，2 女，9 其他
  if(isEmpty(id)) return ''
  if (id.length === 18) {
    return id.charAt(16) % 2 === 0 ? 2 : 1
  } else if (id.length == 15) {
    return id.charAt(14) % 2 === 0 ? 2 : 1
  } else {
    return 9
  }
}

/**
 * 通过身份证号获取生日
 * @param {*} id 身份证号
 * @returns 生日
 */
export function birthdayById(id) {
  let birthday = ''
  if(isEmpty(id)) return ''
  if (id.length == 18) {
    birthday = id.substr(6, 8)
    return birthday.replace(/(.{4})(.{2})/, '$1-$2-')
  } else if (id.length == 15) {
    birthday = '19' + id.substr(6, 6)
    return birthday.replace(/(.{4})(.{2})/, '$1-$2-')
  } else {
    return ''
  }
}

/**
 * 通过年龄计算生日
 * @param {*} age 年龄
 * @param {*} ageType 单位 年(Y) 月(M) 日(D)
 * @returns 生日
 */
export function birthdayByAge(age, ageType) {
  const _date = new Date().getTime()
  const dTime = 86400000
  let ageTime = 0
  if (isEmpty(age)) return ''
  if (ageType === '年' || ageType === '岁' || ageType === 'Y') {
    ageTime = Number(age) * 365 * dTime
  } else if (ageType === '月' || ageType === 'M') {
    ageTime = Number(age) * 30 * dTime
  } else if (ageType === '日' || ageType === '天' || ageType === 'D') {
    ageTime = Number(age) * dTime
  }
  return dateFormatter((_date - ageTime - dTime), false)
}

/**
 * 计算百分比
 * @param {number} value
 * @returns {string}
 */
export function getPercent(num, total) {
  num = parseFloat(num);
  total = parseFloat(total);
  if (isNaN(num) || isNaN(total)) {
    return "-";
  }
  return total <= 0 ? "0%" : (Math.round(num / total * 10000) / 100.00) + "%";
}

/**
 * 密码验证
 * @param {*} val
 * @returns
 */
export function pwdReg(val) {
  return /^(?=.*[a-z])(?=.*[A-Z])(?=.*[$@$!%*?&#^\(\)~])[A-Za-z\d$@$!%*?&#^\(\)~]{6,}/.test(val)
}

/**
 * 密码正则生成
 * @param {*} data 密码正则数据
    * @param {*} 'requireUppercase': true, // 要求大写字母
    * @param {*} 'requireLowercase': true, // 要求小写字母
    * @param {*} 'requireDigit': true, // 要求数字
    * @param {*} 'requireNonAlphanumeric': true, // 要求非字母数字
 * @param {*} minLen 最小长度
 * @returns 返回正则
 */
export function passwordRegSet(minLen = 8, data) {
  let { requireUppercase, requireLowercase, requireDigit, requireNonAlphanumeric } = data
  let reg = ''
  let rega = '[A-Za-z0-9\\W_!@#$%^&*`~\(\)-+=]'
  if (requireUppercase) {
    reg += '(?=.*[A-Z])'
  }
  if (requireLowercase) {
    reg += '(?=.*[a-z])'
  }
  if (requireDigit) {
    reg += '(?=.*[0-9])'
  }
  if (requireNonAlphanumeric) {
    reg += '(?=.*[\\W_!@#$%^&*`~\(\)-+=])'
  }
  return reg ? new RegExp(`^${reg}${rega}{${minLen},}$`) : ''
}
